<template>
  <div>
    <el-input 
      v-model="searchQuery" 
      placeholder="输入用户名搜索" 
      @input="handleSearch" 
      style="margin-bottom: 20px; width: 300px;">
    </el-input>
    
    <el-table :data="filteredTableData" style="width: 100%">
      <el-table-column 
        prop="username" 
        label="用户名" 
        sortable>
      </el-table-column>
      
      <el-table-column 
        prop="password" 
        label="密码" 
        sortable>
      </el-table-column>
      
      <el-table-column 
        prop="createdAt" 
        label="创建时间" 
        sortable 
        width="180">
      </el-table-column>
      
      <el-table-column 
        prop="updatedAt" 
        label="最后修改时间" 
        sortable 
        width="180">
      </el-table-column>
      
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button 
            size="mini" 
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button 
            size="mini" 
            type="danger" 
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination
      style="margin-top: 20px; text-align: right;"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="filteredTableData.length"
      @current-change="handlePageChange">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "UserManagement", // 组件名
  data() {
    return {
      searchQuery: '', // 搜索框的内容
      currentPage: 1, // 当前页
      pageSize: 5, // 每页显示的数量
      tableData: [
        { username: 'admin', password: '123456', createdAt: '2024-01-01', updatedAt: '2024-01-02' },
        { username: 'merchant1', password: '654321', createdAt: '2024-01-03', updatedAt: '2024-01-04' },
        { username: 'merchant2', password: 'abcdef', createdAt: '2024-01-05', updatedAt: '2024-01-06' },
        { username: 'merchant3', password: '123abc', createdAt: '2024-01-07', updatedAt: '2024-01-08' },
        { username: 'merchant4', password: 'password', createdAt: '2024-01-09', updatedAt: '2024-01-10' },
        // 更多商家数据...
      ]
    };
  },
  computed: {
    filteredTableData() {
      // 根据搜索内容过滤数据并支持分页
      const filteredData = this.tableData.filter(item => 
        item.username.includes(this.searchQuery)
      );
      const start = (this.currentPage - 1) * this.pageSize;
      return filteredData.slice(start, start + this.pageSize); // 返回当前页的数据
    }
  },
  methods: {
    handleSearch() {
      // 搜索处理
      this.currentPage = 1; // 搜索后重置为第一页
      console.log('搜索内容:', this.searchQuery);
    },
    handleEdit(index, row) {
      // 编辑商家信息的处理
      console.log('编辑行:', index, row);
    },
    handleDelete(index, row) {
      // 删除商家信息的处理
      this.tableData.splice(index, 1); // 从数据中移除该行
      console.log('删除行:', index, row);
    },
    handlePageChange(page) {
      // 页码变化处理
      this.currentPage = page;
      console.log('当前页:', page);
    }
  }
}
</script>

<style scoped>
/* 添加样式，调整布局 */
.el-input {
  margin-bottom: 20px;
}

.el-table {
  width: 100%;
  margin-bottom: 20px;
}

.el-pagination {
  text-align: right;
}
</style>
